#version 330 core
layout(location =0) in vec3 qt_Vertex;
layout(location =1) in vec2 qt_MultiTexCoord0;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

struct Water
{
    //波长
    float L;
    //振幅
    float A;
    //速度
    float S;
    float t;
    //方向
    vec2 D;
};
uniform Water water0;
uniform Water water1;

uniform Water water[5];

struct VS_OUT
{
    //世界空间坐标
    vec3 vertex;
    vec3 normal;
    //纹理坐标
    vec2 texcoord;
};
out VS_OUT vs_out;

float pi=3.141592653;

void main(void)
{
    vec3 vertexs=qt_Vertex;
    {

        float dfx=0;
        float dfy=0;
        float sinz=0;

        for(int i=0;i<5;++i)
        {
            float w=2*pi/water[i].L;
            float yp=w*water[i].S;
            float t=-water[i].t*yp;
            float A=water[i].A;

            vec2 dir=normalize(water[i].D);
            float l=dot(dir,qt_Vertex.xy);

            sinz+=A*sin(l*w+t);
            float cosz=A*cos(l*w+t);
            dfx+=cosz*dir.x*w;
            dfy+=cosz*dir.y*w;
        }

        mat3 mat;
        mat[0]=model[0].xyz;
        mat[1]=model[1].xyz;
        mat[2]=model[2].xyz;

        //世界坐标切线向量
        //x轴正方向 切线向量
        vec3 vx=mat*vec3(1,0,dfx);
        //y轴正方向 切线向量
        vec3 vy=mat*vec3(0,1,dfy);

        vs_out.normal=cross(vx,vy);
        //vs_out.normal=vs_out.normal*vs_out.normal.z;

        vs_out.normal=normalize(vs_out.normal);

        vertexs.z=vertexs.z + sinz;


        //        float w=(pi/water0.L)*2;
        //        float yp=(pi*water0.S/water0.L)*2;
        //        vec2 dir=normalize(water0.D);
        //        //方向轴变量
        //        float l=dot(dir,qt_Vertex.xy);
        //        //相位差
        //        float t=-water0.t*yp;
        //        float sinz=water0.A * sin(l*w+t);

        //        float dzl=water0.A*cos(l*w+t)*w;

        //        vs_out.normal=vec3(dir.x*-dzl,dir.y*-dzl,1);

        //        mat3 mat;
        //        mat[0]=model[0].xyz;
        //        mat[1]=model[1].xyz;
        //        mat[2]=model[2].xyz;
        //        vs_out.normal=mat*vs_out.normal;

        //        vs_out.normal=normalize(vs_out.normal);


        //        vertexs.z=vertexs.z + sinz;


    }



    //    {
    //        float w=(3.141592653f/water1.L)*2;
    //        float yp=(3.141592653f*water1.S/water1.L)*2;
    //        vertexs.z=vertexs.z + water1.A * sin(dot(normalize(water1.D),qt_Vertex.xy)*w-water1.t*yp);
    //    }

    gl_Position = projection * view * model * vec4(vertexs,1);
    vs_out.texcoord = qt_MultiTexCoord0;
    vs_out.vertex=vec3(model*vec4(vertexs,1.0f));
}
